/**
* Created by wangshuyi on <%= new Date().toLocaleString() %>.
*/

'use strict';
const page = {
editForm : $('#edit-form'),
detailForm : $('#detail-form'),
queryConditionForm: $('#queryConditionForm'),

url : {
list : '<%= path.route %>/list',
remove : '<%= path.route %>/remove/{id}',
save : '<%= path.route %>/save/{id}',
detail : '<%= path.route %>/detail/{id}',
importData : Dolphin.path.contextPath + '<%= path.route %>/import',
exportData : '<%= path.route %>/export',
},

_id : null,
list : null,

init: null,
initElement: null,
initEvent: null,
showDetail: null,
formatterDate: null,
toggleEditState: null,
};

page.init = function () {
page.initElement();
page.initEvent();
};

page.initElement = function () {
const thisPage = this;
Dolphin.form.parse();

thisPage.list = new Dolphin.LIST({
panel : "#datalist",
url : thisPage.url.list,
multiple: false,
title : "<%= modelNameText %>列表",
queryParams : Dolphin.form.getValue('queryConditionForm'),
columns : [<% var count = 0, key; %> <% for (key in field){if(!unLister[key]){ %> <%= count > 0 ? "," : "" %>{
code: "<%= key %>",
title : "<%= nameFormatter[key] || key %>",
}<% count++; }} %>
],
onClick: function (data) {
thisPage.showDetail(data._id);
},
onLoadSuccess: function () {
if(thisPage._id){
this.check(thisPage._id, true);
}
}
});
};


page.initEvent = function () {
const thisPage = this;

//查询
thisPage.queryConditionForm.submit(function () {
thisPage.list.query(Dolphin.form.getValue('queryConditionForm'));
return false;
});

//新增
$('#addData').click(function () {
thisPage._id = "";
thisPage.toggleEditState('edit', true);
});

//修改
$('#editDate').click(function () {
let checkedData = thisPage.list.getChecked();
if(checkedData.length != 1){
Dolphin.alert("请选择一条数据");
}else{
thisPage._id = checkedData[0]._id;
Dolphin.form.setValue(checkedData[0], thisPage.editForm);
thisPage.toggleEditState('edit');
}
});

//删除
$('#removeDate').click(function () {
let checkedData = thisPage.list.getChecked();
if(checkedData.length != 1){
Dolphin.alert("请选择一条数据");
}else{
Dolphin.confirm("确定要删除这条数据吗？", {
callback : function (flag) {
if(flag){
Dolphin.ajax({
url : thisPage.url.remove,
pathData : {id : checkedData[0]._id},
onSuccess : function (reData) {
Dolphin.alert(reData.message, {
callback : function () {
thisPage._id = '';
thisPage.list.reload();
thisPage.toggleEditState('detail', true);
}
})
}
})
}
}
});
}
});

//保存
$('#edit_form_save').click(function () {
let data = Dolphin.form.getValue("edit-form");
Dolphin.ajax({
url : thisPage.url.save,
type : Dolphin.requestMethod.POST,
data : Dolphin.json2string(data),
pathData : {id : thisPage._id},
onSuccess : function (reData) {
Dolphin.alert(reData.message, {
callback : function () {
thisPage._id = reData.data._id;
thisPage.list.reload();
thisPage.showDetail(reData.data._id);
thisPage.toggleEditState('detail');
}
});
}
});
});
//取消
$('#edit_form_cancel').click(function () {
thisPage.toggleEditState();
});

//导入
$('#importData').fileupload({
url: thisPage.url.importData,
dataType: 'json',
done: function (e, data) {
Dolphin.alert(data.result.message, {
callback: function () {
thisPage.list.reload();
}
})
},
progressall: function (e, data) {
// console.log(data);
}
});
//导出
$('#exportDate').click(function () {
window.open(thisPage.url.exportData + '?' + thisPage.queryConditionForm.serialize());
});
};

page.showDetail = function (_id) {
let thisPage = this;
Dolphin.ajax({
url : thisPage.url.detail,
pathData : {id : _id},
//loading : true,
onSuccess : function (reData) {
thisPage.toggleEditState(null, true);
Dolphin.form.setValue(reData.data, thisPage.detailForm, {
formatter : {
createTime : function (val) {
return thisPage.formatterDate(val);
},
updateTime : function (val) {
return thisPage.formatterDate(val);
}
}
});
}
})
};

page.formatterDate = function (val) {
return Dolphin.date2string(new Date(Dolphin.string2date(val, "yyyy-MM-ddThh:mm:ss.").getTime() + 8 * 60 * 60 * 1000), "yyyy-MM-dd hh:mm:ss");
};
page.toggleEditState = function (state, flag) {
state = state || 'detail';
flag = flag || false;
let thisPage = this;
if(flag){
Dolphin.form.empty(thisPage.detailForm);
Dolphin.form.empty(thisPage.editForm);
}
switch(state){
case 'edit':
thisPage.detailForm.hide();
thisPage.editForm.show();
break;
case 'detail':
thisPage.detailForm.show();
thisPage.editForm.hide();
break;
}
};


$(function () {
page.init();
});